
#include "qyZipPackage.h"
#include "qyZipFileFilter.h"
#include "../lex/qyLex.h"

//-----------------------------------------------------------------------
qyZipPackage::qyZipPackage()
{

}

//-----------------------------------------------------------------------
qyZipPackage::~qyZipPackage()
{
	Release();	
}

//-----------------------------------------------------------------------
void qyZipPackage::Release()
{
}

//-----------------------------------------------------------------------
bool qyZipPackage::SetPackageFile( const qyString &path )
{
	m_path = path;
	Release();
	m_zipHandle = unzOpen( path.c_str() );
	if ( m_zipHandle == NULL )
	{
		return false;
	}
	m_zipFilter._SetupZip( m_zipHandle );
	return true;
}

//-----------------------------------------------------------------------
void qyZipPackage::ParseTableOfContents()
{
	int walkRes = unzGoToFirstFile( m_zipHandle );
	if (UNZ_OK == walkRes) do
	{
		// get info about current file
		char curFileName[512];
		unz_file_info fileInfo;
		int fileInfoRes = unzGetCurrentFileInfo( m_zipHandle,
			&fileInfo,
			curFileName,
			sizeof(curFileName),
			0, 0, 0, 0);
		assert(UNZ_OK == fileInfoRes);
		_AddEntry(curFileName);
		walkRes = unzGoToNextFile( m_zipHandle );
	}
	while (UNZ_OK == walkRes);
	if (UNZ_END_OF_LIST_OF_FILE != walkRes)
	{
	}
}

//-----------------------------------------------------------------------
bool qyZipPackage::OpenReadFile( qyFile &file, const qyString &relativePath )
{
	return m_zipFilter.LoadFileToMemory( relativePath, file );
}

//-----------------------------------------------------------------------
qyZipPackage::PathList qyZipPackage::GetFileListInPktDir( const qyString &dirInZip )
{
	PathList pathList;
	return pathList;
}

//-----------------------------------------------------------------------
qyZipPackage::PathList qyZipPackage::GetDirListInPktDir( const qyString &dirInZip )
{
	PathList pathList;
	return pathList;
}

//-----------------------------------------------------------------------
void qyZipPackage::_AddEntry( const qyString &path )
{
	/*
	bool isDirectory = false;
	char lastChar = path[strlen(path.c_str()) - 1];
	if ((lastChar == '/') || (lastChar == '\\'))
	{
		isDirectory = true;
	}
	*/
	qyTokenSep sep( "/\\" );
	qyTokenizer pathTokens( path, sep );

	qyZipEntry* dirEntry = &(m_rootEntry);
	qyTokenIterator tok_it = pathTokens.begin();
	for ( ; tok_it!=pathTokens.end();++tok_it )
	{
		qyZipEntry *childDirEntry = dirEntry->FindDirEntry( *tok_it );
		if ( childDirEntry == 0 )
		{
			childDirEntry = dirEntry->AddSubEntry( path, *tok_it );
		}
		dirEntry = childDirEntry;
	}
	/*
	if (pathTokens.() > 1)
	{
		// find directory, create missing directory entries on the way
		int i;
		for (i = 0; i < (pathTokens.size() - 1); i++)
		{
			StringAtom curToken = pathTokens[i];
			ZipDirEntry* childDirEntry = dirEntry->FindDirEntry(curToken);
			if (0 == childDirEntry)
			{
				// need to create new dir entry
				childDirEntry = dirEntry->AddDirEntry(curToken);
			}
			dirEntry = childDirEntry;
		}
	}

	// create final entry and add to last dir entry
	StringAtom finalName(pathTokens.Back());
	if (isDirectory)
	{
		dirEntry->AddDirEntry(finalName);
	}
	else
	{
		ZipFileEntry* finalFileEntry = dirEntry->AddFileEntry(finalName);
		finalFileEntry->Setup(finalName, this->zipFileHandle, &this->archiveCritSect);
	}
	*/
}

//-----------------------------------------------------------------------

